<template>
	<el-dialog v-model="dialog" :title="form.id ? '修改部门' : '添加部门'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="form" label-width="120px" :rules="rules">
			<el-form-item label="部门名称" prop="deptName">
				<el-input v-model="form.deptName"></el-input>
			</el-form-item>
			<el-form-item label="上级部门" prop="parentId" v-if="form.parentId != '0'">
				<el-tree-select
					style="width: 100%"
					v-model="form.parentId"
					:data="deptTree"
					check-strictly
					:props="defaultProps"
					:render-after-expand="false"
					node-key="id"
				></el-tree-select>
			</el-form-item>
			<el-form-item label="负责人" prop="leader">
				<el-input v-model="form.leader"></el-input>
			</el-form-item>
			<el-form-item label="手机号" prop="leaderPhone">
				<el-input v-model="form.leaderPhone"></el-input>
			</el-form-item>
			<el-form-item label="排序序号" prop="sort">
				<el-input-number v-model="form.sort" :min="0"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="enableFlag">
				<el-radio-group v-model="form.enableFlag">
					<el-radio label="0">禁用</el-radio>
					<el-radio label="1">启用</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { addObj, editObj, getById } from '/@/api/upms/sysdept';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
export default defineComponent({
	emits: ['initPage'],
	setup(props, { emit }) {
		const data = reactive({
			form: { id: undefined, deptName: '', parentId: '', leader: '', leaderPhone: '', sort: 0, enableFlag: '1' },
			rules: {
				deptName: [
					{
						required: true,
						message: '请输入部门名称',
						trigger: 'change',
					},
				],
				parentId: [
					{
						required: true,
						message: '请选择上级部门',
						trigger: 'change',
					},
				],
				leader: [
					{
						required: true,
						message: '请输入负责人',
						trigger: 'change',
					},
				],
				sort: [
					{
						required: true,
						message: '请输入排序序号',
						trigger: 'change',
					},
				],
				enableFlag: [
					{
						required: true,
						message: '请选择状态',
						trigger: 'change',
					},
				],
			},
			deptTree: [],
		});
		const dialog = ref(false);
		const loading = ref(false);
		const formRef = ref();
		const defaultProps = {
			children: 'children',
			label: 'name',
		};
		const initForm = (row: any) => {
			dialog.value = true;
			if (row && row.id) {
				getDetail(row.id);
			}
		};
		const getDetail = (id: string) => {
			loading.value = true;
			// 修改
			getById(id)
				.then((response: any) => {
					loading.value = false;
					data.form = response.data.data;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 关闭事件
		 */
		const handleClose = () => {
			resetForm(formRef.value);
		};
		/**
		 * 重置表单
		 */
		const resetForm = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			data.form.id = undefined;
			loading.value = false;
			dialog.value = false;
			formEl.resetFields();
		};
		/**
		 * 提交按钮
		 */
		const submitForm = async (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			await formEl.validate((valid, fields) => {
				if (valid) {
					loading.value = true;
					if (data.form.id) {
						//修改
						edit();
					} else {
						//新增
						add();
					}
				}
			});
		};
		/**
		 * 新增
		 */
		const add = () => {
			addObj(data.form)
				.then((response: any) => {
					resetForm(formRef.value);
					ElMessage.success('新增成功');
					emit('initPage');
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 修改
		 */
		const edit = () => {
			editObj(data.form)
				.then((response: any) => {
					resetForm(formRef.value);
					ElMessage.success('修改成功');
					emit('initPage');
				})
				.catch(() => {
					loading.value = false;
				});
		};

		return {
			dialog,
			loading,
			formRef,
			defaultProps,
			initForm,
			handleClose,
			submitForm,
			add,
			edit,
			...toRefs(data),
		};
	},
});
</script>
